Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls


<Assembly: TagPrefix("CaseStudyControls", "csc")> 
<ToolboxData("<{0}:CustomerInformation runat=server></{0}:CustomerInformation>")> _
Public Class CustomerInformation
    Inherits System.Web.UI.WebControls.WebParts.WebPart

    Public Enum ciModeSettings
        ciUpdate
        ciDisplay
    End Enum

    Private _Mode As ciModeSettings

    <DefaultValue(ciModeSettings.ciDisplay), Category("Behavior")> _
    Public Property Mode() As ciModeSettings
        Get
            Return _Mode
        End Get
        Set(ByVal value As ciModeSettings)
            _Mode = value
        End Set
    End Property


    Protected Overrides Sub CreateChildControls()
        Dim lblName As New System.Web.UI.WebControls.Label
        Dim lblEmail As New System.Web.UI.WebControls.Label
        Dim lblStreet As New System.Web.UI.WebControls.Label
        Dim lblCity As New System.Web.UI.WebControls.Label
        Dim lblStateProvince As New System.Web.UI.WebControls.Label
        Dim lblCountry As New System.Web.UI.WebControls.Label
        Dim lblNameLb As New System.Web.UI.WebControls.Label
        Dim lblEmailLb As New System.Web.UI.WebControls.Label
        Dim lblStreetLb As New System.Web.UI.WebControls.Label
        Dim lblCityLb As New System.Web.UI.WebControls.Label
        Dim lblStateProvinceLb As New System.Web.UI.WebControls.Label
        Dim lblCountryLb As New System.Web.UI.WebControls.Label

        Dim txtName As New System.Web.UI.WebControls.TextBox
        Dim txtEmail As New System.Web.UI.WebControls.TextBox
        Dim txtStreet As New System.Web.UI.WebControls.TextBox
        Dim txtCity As New System.Web.UI.WebControls.TextBox
        Dim txtStateProvince As New System.Web.UI.WebControls.TextBox
        Dim txtCountry As New System.Web.UI.WebControls.TextBox

        Dim st As System.Web.UI.WebControls.Style
        Dim tbl As New System.Web.UI.WebControls.Table
        Dim pnl As New System.Web.UI.WebControls.Panel
        pnl.Height = 150
        pnl.Width = 320

        Me.ChildControlsCreated = True
        If Me.ControlStyleCreated = True Then
            st = Me.ControlStyle
        Else
            st = New System.Web.UI.WebControls.Style
        End If
        If st.BorderStyle = WebControls.BorderStyle.NotSet Then
            st.BorderColor = Drawing.Color.Black
            st.BorderWidth = 1
            st.BorderStyle = WebControls.BorderStyle.Solid
        End If

        lblName.ID = "lblName"
        lblName.Attributes("DisplayType") = "label"
        lblEmail.ID = "lblEmail"
        lblEmail.Attributes("DisplayType") = "label"
        lblStreet.ID = "lblStreet"
        lblStreet.Attributes("DisplayType") = "label"
        lblCity.ID = "lblCity"
        lblCity.Attributes("DisplayType") = "label"
        lblStateProvince.ID = "lblStatProv"
        lblStateProvince.Attributes("DisplayType") = "label"
        lblCountry.ID = "Country"
        lblCountry.Attributes("DisplayType") = "label"

        If Me.Mode = ciModeSettings.ciUpdate Then
            txtName.ID = "txtName"
            txtName.Attributes("DisplayType") = "data"
            txtName.Width = 100
            txtName.MergeStyle(st)

            txtEmail.ID = "txtEmail"
            txtEmail.Attributes("DisplayType") = "data"
            txtEmail.Width = 100
            txtEmail.MergeStyle(st)

            txtStreet.ID = "txtStreet"
            txtStreet.Attributes("DisplayType") = "data"
            txtStreet.Width = 100
            txtStreet.MergeStyle(st)

            txtCity.ID = "txtCity"
            txtCity.Attributes("DisplayType") = "data"
            txtCity.Width = 100
            txtCity.MergeStyle(st)

            txtCountry.ID = "txtCountry"
            txtCountry.Attributes("DisplayType") = "data"
            txtCountry.Width = 100
            txtCountry.MergeStyle(st)

            txtStateProvince.ID = "txtStateProvince"
            txtStateProvince.Attributes("DisplayType") = "data"
            txtStateProvince.Width = 100
            txtStateProvince.MergeStyle(st)

            txtCountry.ID = "txtCountry"
            txtCountry.Attributes("DisplayType") = "data"
            txtCountry.Width = 100
            txtCountry.MergeStyle(st)
        Else
            lblNameLb.Text = "Name:"
            lblNameLb.Attributes("DisplayType") = "data"
            lblEmailLb.Text = "Email:"
            lblEmailLb.Attributes("DisplayType") = "data"
            lblStreetLb.Text = "Street:"
            lblStreetLb.Attributes("DisplayType") = "data"
            lblCityLb.Text = "City:"
            lblCityLb.Attributes("DisplayType") = "data"
            lblStateProvinceLb.Text = "State/Province:"
            lblStateProvinceLb.Attributes("DisplayType") = "data"
            lblCountryLb.Text = "Country:"
            lblCountryLb.Attributes("DisplayType") = "data"
        End If

        'use this routine without the table positioning code
        If Me.Mode = ciModeSettings.ciUpdate Then
            Me.Controls.Clear()
            Me.Controls.Add(lblNameLb)
            Me.Controls.Add(lblName)
            Me.Controls.Add(lblEmailLb)
            Me.Controls.Add(lblEmail)
            Me.Controls.Add(lblStreetLb)
            Me.Controls.Add(lblStreet)
            Me.Controls.Add(lblCityLb)
            Me.Controls.Add(lblCity)
            Me.Controls.Add(lblStateProvinceLb)
            Me.Controls.Add(lblStateProvince)
            Me.Controls.Add(lblCountryLb)
            Me.Controls.Add(lblCountry)
        Else
            Me.Controls.Clear()
            Me.Controls.Add(lblNameLb)
            Me.Controls.Add(txtName)
            Me.Controls.Add(lblEmailLb)
            Me.Controls.Add(txtEmail)
            Me.Controls.Add(lblStreetLb)
            Me.Controls.Add(txtStreet)
            Me.Controls.Add(lblCityLb)
            Me.Controls.Add(txtCity)
            Me.Controls.Add(lblStateProvinceLb)
            Me.Controls.Add(txtStateProvince)
            Me.Controls.Add(lblCountryLb)
            Me.Controls.Add(txtCountry)
        End If

        'use this instead of the Controls.add code or AddPositioning code
        If Me.Mode = ciModeSettings.ciUpdate Then
            tbl = AddToTable(tbl, lblNameLb, lblName)
            tbl = AddToTable(tbl, lblEmailLb, lblEmail)
            tbl = AddToTable(tbl, lblStreetLb, lblCity)
            tbl = AddToTable(tbl, lblStreetLb, lblCity)
            tbl = AddToTable(tbl, lblStateProvinceLb, lblStateProvince)
            tbl = AddToTable(tbl, lblCountryLb, lblCountry)
        Else
            tbl = AddToTable(tbl, lblNameLb, txtName)
            tbl = AddToTable(tbl, lblEmailLb, txtEmail)
            tbl = AddToTable(tbl, lblStreetLb, txtCity)
            tbl = AddToTable(tbl, lblStreetLb, txtCity)
            tbl = AddToTable(tbl, lblStateProvinceLb, txtStateProvince)
            tbl = AddToTable(tbl, lblCountryLb, txtCountry)
        End If
        Me.Controls.Add(tbl)

        'use this instead of the Controls.add code or AddToTable code
        If Me.Mode = ciModeSettings.ciUpdate Then
            AddPositioning(0, lblNameLb, lblName)
            AddPositioning(1, lblEmailLb, lblEmail)
            AddPositioning(2, lblStreetLb, lblStreet)
            AddPositioning(3, lblCityLb, lblCity)
            AddPositioning(4, lblStateProvinceLb, lblStateProvince)
            AddPositioning(5, lblCountryLb, lblCountry)
            pnl.Controls.Add(lblName)
            pnl.Controls.Add(lblEmail)
            pnl.Controls.Add(lblStreet)
            pnl.Controls.Add(lblCity)
            pnl.Controls.Add(lblStateProvince)
            pnl.Controls.Add(lblCountry)
        Else
            AddPositioning(0, lblNameLb, txtName)
            AddPositioning(1, lblEmailLb, txtEmail)
            AddPositioning(2, lblStreetLb, txtStreet)
            AddPositioning(3, lblCityLb, txtCity)
            AddPositioning(4, lblStateProvinceLb, txtStateProvince)
            AddPositioning(5, lblCountryLb, txtCountry)
            pnl.Controls.Add(txtName)
            pnl.Controls.Add(txtEmail)
            pnl.Controls.Add(txtStreet)
            pnl.Controls.Add(txtCity)
            pnl.Controls.Add(txtStateProvince)
            pnl.Controls.Add(txtCountry)
        End If
        pnl.Controls.Add(lblNameLb)
        pnl.Controls.Add(lblEmailLb)
        pnl.Controls.Add(lblStreetLb)
        pnl.Controls.Add(lblCityLb)
        pnl.Controls.Add(lblStateProvinceLb)
        pnl.Controls.Add(lblCountryLb)
        Me.Controls.Add(pnl)

    End Sub

    Private Function AddToTable(ByVal Table As System.Web.UI.WebControls.Table, _
             ByVal Label As System.Web.UI.WebControls.Label, _
             ByVal Data As System.Web.UI.WebControls.WebControl) As WebControls.Table
        Dim tc As System.Web.UI.WebControls.TableCell
        Dim tr As System.Web.UI.WebControls.TableRow

        tr = New System.Web.UI.WebControls.TableRow
        tc = New System.Web.UI.WebControls.TableCell
        tc.Controls.Add(Label)
        tr.Cells.Add(tc)

        tc = New System.Web.UI.WebControls.TableCell
        tc.Controls.Add(Data)
        tr.Cells.Add(tc)
        Table.Rows.Add(tr)

        Return Table

    End Function

    Private Sub AddPositioning(ByVal LineNumber As Integer, _
                 ByVal Label As System.Web.UI.WebControls.Label, _
                 ByVal Data As System.Web.UI.WebControls.WebControl)
        Dim intVerticalOffset As Integer

        intVerticalOffset = LineNumber * 25
        Label.Style.Add("position", "absolute")
        Label.Style.Add("top", intVerticalOffset.ToString & "px")
        Label.Style.Add("left", "0px")
        Data.Style.Add("position", "absolute")
        Data.Style.Add("top", intVerticalOffset.ToString & "px")
        Data.Style.Add("left", "70px")

    End Sub

    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        'Use this routine if absolute positioning or the table method aren't being used
        For Each wc As System.Web.UI.WebControls.WebControl In Me.Controls
            Select Case wc.Attributes("DisplayType")
                Case "data"
                    wc.RenderControl(writer)
                    writer.WriteBreak()
                Case "label"
                    wc.RenderControl(writer)
                    writer.Write(" ")
            End Select
        Next

    End Sub

    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
        MyBase.OnInit(e)
        If Me.DesignMode = True Then
            Me.EnsureChildControls()
        End If
    End Sub

End Class
